﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.CanGongService;
using CommunitySystem.Ctrls;
using CommunitySystem.EmpbasicService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class CanGongControl : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            LoadData();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        public ObservableCollection<CanGongModel> models = new ObservableCollection<CanGongModel>();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public CanGongService.CanGongServiceClient client = new CanGongServiceClient();
        public CanGongControl()
        {
            InitializeComponent();
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.GetDengLuOrgCompleted += new EventHandler<GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
            client.GetCanGongModelCompleted += new EventHandler<GetCanGongModelCompletedEventArgs>(client_GetCanGongModelCompleted);
            client.DeleteCanGongCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteCanGongCompleted);
            client.GetCanGongDetailModel1Completed += new EventHandler<GetCanGongDetailModel1CompletedEventArgs>(client_GetCanGongDetailModel1Completed);
            LoadData();
        }
        public void LoadData()
        {
            client.GetCanGongModelAsync(0, orgids);
        }

        void client_GetCanGongModelCompleted(object sender, GetCanGongModelCompletedEventArgs e)
        {
            models = e.Result;
            DGRole.ItemsSource = e.Result;
        }

        private string juese = "";
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            juese = e.Result;
            if (e.Result != "管理员")
            {
                NewData_admin.Visibility = System.Windows.Visibility.Collapsed;
                EditData_admin.Visibility = System.Windows.Visibility.Collapsed;
                DeleteData_admin.Visibility = System.Windows.Visibility.Collapsed;
                QueRen.Visibility = System.Windows.Visibility.Collapsed;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
            }
            else
            {
                NewData_admin.Visibility = System.Windows.Visibility.Visible;
                EditData_admin.Visibility = System.Windows.Visibility.Visible;
                DeleteData_admin.Visibility = System.Windows.Visibility.Visible;
                QueRen.Visibility = System.Windows.Visibility.Visible;
            }
        }

        public string orgids = "";
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            orgids = e.Result;
            LoadData();
            
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            CanGongWindow window = new CanGongWindow(0, true);
            window.Closed += new EventHandler(RMWindow_Closed);
            window.Show();
        }

        void RMWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = DGRole.SelectedItem as CanGongModel;
            if(seljw != null)
            {
                CanGongWindow window = new CanGongWindow(seljw.ID, false);
                window.Closed += new EventHandler(RMWindow_Closed);
                window.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位进行编辑！");
            }
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = DGRole.SelectedItem as CanGongModel;
            if (seljw != null)
            {
                if (MessageBox.Show("确定删除该参公过渡单位吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client.DeleteCanGongAsync(seljw.ID,seljw.OrgID);
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位进行删除！");
            }
        }
        void client_DeleteCanGongCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.ErrorMessage("删除成功！");
            LoadData();
        }

        private void Data_Click(object sender, RoutedEventArgs e)
        {
            var seljw = DGRole.SelectedItem as CanGongModel;
            if (seljw != null)
            {
                CanGongDetailControl view = new CanGongDetailControl(seljw.OrgID, seljw.Status);
                Nav.ShowNextPage(seljw.Name + "-参公过渡人员情况", view, this);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请先选择一个参公过渡单位用于查看人员！");
            }
        }

        private CanGongModel sel = new CanGongModel();
        private void QueRen_Click(object sender, RoutedEventArgs e)
        {
            sel = DGRole.SelectedItem as CanGongModel;
            if (sel != null)
            {
                client.GetCanGongDetailModel1Async(sel.ID, 0);

            }
        }

        void client_GetCanGongDetailModel1Completed(object sender, GetCanGongDetailModel1CompletedEventArgs e)
        {
            if(e.Result == null || e.Result.Count == 0)
            {
                if (MessageBox.Show("确认过渡吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Can window = new Can(sel);
                    window.Closed += new EventHandler(RMWindow_Closed);
                    window.Show();
                }
            }
            else
            {
                if (MessageBox.Show("所选单位还有未提交人员，如确认过渡则未提交人员不被过渡。确认过渡吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Can window = new Can(sel);
                    window.Closed += new EventHandler(RMWindow_Closed);
                    window.Show();
                }
            }
        }
    }
}
